#include "myserialport.h"
#include <QDebug>
MySerialPort::MySerialPort(QObject *parent) : QObject(parent)
{
    m_serial = new QSerialPort;
}

bool MySerialPort::serialConnect(QString port)
{
    m_serial->setPortName(port);
    if(!m_serial->open(QIODevice::ReadWrite))
    {
        printf("Open Error");
        return 0;
    }
    m_serial->setBaudRate(QSerialPort::Baud115200);     //设置波特率为115200
    m_serial->setDataBits(QSerialPort::Data8);          //设置数据位8
    m_serial->setParity(QSerialPort::NoParity);         //校验位设置为0
    m_serial->setStopBits(QSerialPort::OneStop);        //停止位设置为1
    m_serial->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制
    return 1;
}
QString MySerialPort::getSerialInfo(){
    foreach (const QSerialPortInfo &Info, QSerialPortInfo::availablePorts()) {
        qDebug() << "portName    :"  << Info.portName();//调试时可以看的串口信息
//        qDebug() << "Description   :" << Info.description();
        emit sendSerialInfo(Info.portName());
    }
    return "hello world";
}
QString MySerialPort::serialRead()
{
    return m_serial->readAll();
}

bool MySerialPort::serialWrite(QString sendStr)
{
    if(m_serial->write(sendStr.toLatin1().data(),strlen(sendStr.toLatin1().data())))
        return 1;
    else
        return 0;
}
bool MySerialPort::isOpen(){
    return m_serial->isOpen();
}
void MySerialPort::serialClose()
{
    if(m_serial->isOpen())
    {
        m_serial->clear();
        m_serial->close();
    }
}
MySerialPort::~MySerialPort()
{
    if(m_serial->isOpen())
    {
        m_serial->clear();
        m_serial->close();
    }
    m_serial->deleteLater();
}
